<%	
    # Create the helper
  	flex_scaffold_helper = FlexScaffoldHelper.new("#{actions.first.camelcase}")
   
-%>package <%= class_name.downcase.gsub('::','.') %>.vo
{
	import org.genuflex.utils.GenuflexDataFormatter;
	import org.genuflex.vo.GenuflexMetadata;	
	import org.genuflex.vo.GenuflexValueObject;
	
<%
	#Import any classes for a belongs-to relationship
	if !flex_scaffold_helper.belongs_to_class_names.nil?
		flex_scaffold_helper.belongs_to_class_names.values.each do | belongs_to_assoc |
%>	
	import <%= class_name.downcase.gsub('::','.') %>.vo.<%= belongs_to_assoc.class_name %>VO;<%
		end
	end

	#Import any classes for a has-many relationship
	flex_scaffold_helper.has_many_associations.each do | assoc |
%>	
	import <%= class_name.downcase.gsub('::','.') %>.vo.<%= assoc.klass().to_s %>VO;<%
	end

	#Import any classes for a habtm relationship
	flex_scaffold_helper.habtm_associations.each do | assoc |
%>	
	import <%= class_name.downcase.gsub('::','.') %>.vo.<%= assoc.klass().to_s %>VO;<%
	end
%>

	[Bindable]
	[RemoteClass(alias="<%= class_name.downcase.gsub('::','.') %>.vo.<%= actions.first.camelcase %>VO")]
	public class <%= actions.first.camelcase %>VO extends GenuflexValueObject
	{
		public static var metadata : GenuflexMetadata;
		
<%
		# Iterate through the columns
		flex_scaffold_helper.object_columns.each do | object_column |
			flex_type = flex_scaffold_helper.get_flex_type(object_column)

%>		public var <%=object_column.name%> : <%=flex_type%>;
<%
		end
		
		# Now, add any belongs_to variables

		flex_scaffold_helper.belongs_to_class_names.values.each do | belongs_to_assoc |
		# Create the object to recieve a belongs_to related object
%>		[Transient] public var <%=belongs_to_assoc.name%> : <%=belongs_to_assoc.class_name%>VO;
<%
		end

		# Now, add any has_many variables

		flex_scaffold_helper.has_many_associations.each do | has_many_assoc |
		  #Don't add these functions if it's a has_many through relationship
		  if !has_many_assoc.options.key?(:through) 
		
		# Create the Array to recieve the has_many related object collection
%>		[Transient] public var <%=has_many_assoc.name%> : Array;
<%
			end # not has_many :through
		end

		# Now, add any has_and_belongs_to_many variables

		flex_scaffold_helper.habtm_associations.each do | habtm_assoc |
		# Create the Array to recieve the has_and_belongs_to_many related object collection
%>		public var <%=habtm_assoc.klass().to_s.underscore.downcase %>_ids : Array;
		[Transient] public var <%=habtm_assoc.name%> : Array;
<%
		end


	%>
		public override function getLabel() : String {
			return <%=flex_scaffold_helper.get_label_function_code(actions.first.camelcase) %>;
		}

		public static function labelFunction(item : Object, other : * = null) : String {
			if (item is <%= actions.first.camelcase %>VO) {
				return <%= actions.first.camelcase %>VO(item).getLabel();
			}
			else if (item is String) {
				return item as String;
			}
			else if (item.hasOwnProperty("<%=flex_scaffold_helper.source %>") && item["<%=flex_scaffold_helper.source %>"] is <%= actions.first.camelcase %>VO) {
				return <%= actions.first.camelcase %>VO(item["<%=flex_scaffold_helper.source %>"]).getLabel();
			}
			else {
				return "Unknown <%=flex_scaffold_helper.title %>";
			}
		}
		
		public function <%= actions.first.camelcase %>VO() {
			if (metadata == null) {
				metadata = new GenuflexMetadata();				
<%					
	flex_scaffold_helper.belongs_to_class_names.values.each do | assoc |
 		model_property = flex_scaffold_helper.create_obj_name(assoc.class_name).pluralize

%>
				metadata.addBelongsToAssociation(<%= actions.first.camelcase %>VO, "<%=assoc.primary_key_name%>", "<%=assoc.name%>", <%= assoc.klass().to_s %>VO);<%

 	end #belongs_to_class_names.each

	# Add calls to the data cache for any has-many relationships
	flex_scaffold_helper.has_many_associations.each do | assoc |

		if !assoc.options.key?(:through)
%>	
				metadata.addHasManyAssociation(<%= actions.first.camelcase %>VO, "<%=assoc.name %>", <%= assoc.klass().to_s %>VO, "<%=assoc.primary_key_name%>");<%


		end #if not has_many :through
	end

	# Add calls to the data cache for any has-many relationships
	flex_scaffold_helper.habtm_associations.each do | assoc |
		habtm_name = flex_scaffold_helper.get_habtm_name(assoc)

		if !habtm_name.nil?
%>	
				metadata.addHasAndBelongsToManyAssociation(<%= actions.first.camelcase %>VO, "<%=assoc.name%>", <%= assoc.klass().to_s %>VO, "<%=habtm_name %>");<%

		end #if habtm_name not nil
	end

%>
			}
		}
	}
}